<?php

class EventManager extends Page {
	
	function __construct($pkPage) {
		parent::__construct($pkPage);
	}
	
	protected function loadSecurityObjects() {

		parent::loadSecurityObjects();
	
	}
	
	protected function addPageMethods() {
		
		parent::addPageMethods();	
		
		if($this->canItemRead()) {
			$this->allowMethod("detail");
		}
	}
	
	function index() {
	
		//Load the first (Default: 30) Log Entries to the
		$cfg = Runtime::getConfiguration();
		
		$t = new TableAdapter($cfg['table_prefix']."EventLog");
		$sql = "SELECT l.*, e.name AS 'name', e.level AS 'level', e.logText as 'logText', 
					   mi.name AS 'instanceName', 
					   m.name AS 'moduleName', 
					   u.realname AS 'userRealName', 
					   $this->fkInstanceSecurityObject AS 'fkSecurityObject' FROM {table} AS l 
				LEFT JOIN " . $cfg['table_prefix'] . "Event AS e ON e.pkEvent = l.fkEvent 
				LEFT JOIN " . $cfg['table_prefix'] . "ModuleInstance AS mi ON mi.pkModuleInstance = l.fkModuleInstance 
				LEFT JOIN " . $cfg['table_prefix'] . "Module AS m ON m.pkModule = mi.fkModule 
				LEFT JOIN " . $cfg['table_prefix'] . "SecurityUser AS u ON u.pkSecurityUser = l.fkUser 
				ORDER BY l.logDate DESC LIMIT %s";
		$Items = $t->getAllByQuery($sql, array($this->getSetting("EventPerPage", 30)), "EventLogItem");
		
		
		$this->addViewData("Items", $Items);
		
		//print_r(runtime::getCurrentPage());
				
	}
	
	function detail() {

		//Load the first (Default: 30) Log Entries to the
		$cfg = Runtime::getConfiguration();
		
		$pkEventLog = $this->pageRequest->getGetVar("pkEventLog");
		
		$t = new TableAdapter($cfg['table_prefix']."EventLog");
		$sql = "SELECT l.*, e.name AS 'name', e.level AS 'level', e.logText as 'logText', 
					   mi.name AS 'instanceName', 
					   m.name AS 'moduleName', 
					   u.realname AS 'userRealName', 
					   $this->fkInstanceSecurityObject AS 'fkSecurityObject' FROM {table} AS l 
				LEFT JOIN " . $cfg['table_prefix'] . "Event AS e ON e.pkEvent = l.fkEvent 
				LEFT JOIN " . $cfg['table_prefix'] . "ModuleInstance AS mi ON mi.pkModuleInstance = l.fkModuleInstance 
				LEFT JOIN " . $cfg['table_prefix'] . "Module AS m ON m.pkModule = mi.fkModule 
				LEFT JOIN " . $cfg['table_prefix'] . "SecurityUser AS u ON u.pkSecurityUser = l.fkUser 
				WHERE pkEventLog = %n";

				
		$Item = $t->getSingleByQuery($sql, array($pkEventLog), "EventLogItem");
		
		$this->addViewData("Item", $Item);
		$this->addViewData("vardata", unserialize($Item->vardata));
		
	}
	
	function edit() {

	}
	
	function editEvent() {

	}
	
	function deleteEvent() {
		
	}
	
	

}

/*
 * MODEL
 */
	
class EventLogItem extends ResultItem {
	
	public function __construct($data) {

		$data["foo"] = "bar";
		
		parent::__construct($data);
	}
	
}
	?>